﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AG.Editor.Core.Data;
using System.IO;
using System.Xml.Linq;

namespace AG.Editor.Core.Publish
{
    public class AGModelPulisher
    {
        /// <summary>
        /// 发布模型文件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="publishPath">发布的目录</param>
        public void Publish(AGEProject project, AGModel model, string publishPath)
        {
            string modelFolder = project.GetFolder(model);
            string modelFile = string.Format("{0}\\{1}.xml", project.GetFolder(), model.UniqueId);
            string pubModelFolder = string.Format("{0}\\{1:d4}", publishPath, model.Id);
            string pubModelFile = string.Format("{0}\\{1:d4}.xml", publishPath, model.Id);

            if (!Directory.Exists(pubModelFolder))
            {
                Directory.CreateDirectory(pubModelFolder);
            }

            #region // 拷贝文件
            for (int indexAction = 0; indexAction < model.Actions.Count; indexAction++)
            {
                AGAction objAction = model.Actions[indexAction];
                for (int indexDirection = 0; indexDirection < objAction.Directions.Count; indexDirection++)
                {
                    AGDirection objDirection = objAction.Directions[indexDirection];
                    if (objDirection.RefDirection != null)
                    {
                        continue;
                    }
                    for (int indexFrame = 0; indexFrame < objDirection.Frames.Count; indexFrame++)
                    {
                        AGFrame objFrame = objDirection.Frames[indexFrame];
                        if (objFrame.RefFrame != null)
                        {
                            continue;
                        }
                        string sourceFilePath = string.Format("{0}\\{1}", modelFolder, objFrame.ImageFileName);
                        FileInfo sourceFileInfo = new FileInfo(sourceFilePath);
                        string destFilePath = string.Format("{0}\\{1}", pubModelFolder, sourceFileInfo.Name);
                        File.Copy(sourceFilePath, destFilePath, true);
                    }
                }
            }
            #endregion

            #region 生成模型文件
            XDocument xDoc = new XDocument();
            XElement xModel = new XElement("model");
            xModel.Add(new XAttribute("id", model.Id));
            xModel.Add(new XAttribute("caption", model.Caption));
            xModel.Add(new XAttribute("version", "1.0"));
            xDoc.Add(xModel);

            for (int indexAction = 0; indexAction < model.Actions.Count; indexAction++)
            {
                AGAction objAction = model.Actions[indexAction];
                XElement xAction = new XElement("action");
                xModel.Add(xAction);

                xAction.Add(new XAttribute("id", objAction.Id));

                for (int indexDirection = 0; indexDirection < objAction.Directions.Count; indexDirection++)
                {
                    AGDirection objDirection = objAction.Directions[indexDirection];
                    // 真实的输出方向，当是引用模式的时候指定到引用的方向上
                    AGDirection actualDirection = objDirection;
                    if (objDirection.RefDirection != null)
                    {
                        actualDirection = objDirection.RefDirection;
                    }
                    List<AGFrame> frames = objDirection.GetFrames();

                    XElement xDirection = new XElement("direction");
                    xAction.Add(xDirection);

                    xDirection.Add(new XAttribute("id", objDirection.Id));

                    for (int indexFrame = 0; indexFrame < frames.Count; indexFrame++)
                    {
                        AGFrame objFrame = frames[indexFrame];
                        XElement xFrame = new XElement("frame");
                        xDirection.Add(xFrame);

                        string sourceFilePath = string.Format("{0}\\{1}", modelFolder, objFrame.ImageFileName);
                        FileInfo sourceFileInfo = new FileInfo(sourceFilePath);
                        string destFilePath = objFrame.ImageFileName.Replace(sourceFileInfo.Extension, string.Empty);
                        xFrame.Add(new XAttribute("id", indexFrame));
                        /*
                        xFrame.Add(new XAttribute("width", objFrame.Width));
                        xFrame.Add(new XAttribute("height", objFrame.Width));
                        xFrame.Add(new XAttribute("anchor-x", objFrame.AnchorPointX));
                        xFrame.Add(new XAttribute("anchor-y", objFrame.AnchorPointY));
                        xFrame.Add(new XAttribute("path", destFilePath));
                        */
                        xFrame.Add(new XAttribute("rect", string.Format("0,0,{0},{1}", objFrame.Width, objFrame.Height)));
                        xFrame.Add(new XAttribute("pivot", string.Format("{0},{1}", objFrame.AnchorPointX, objFrame.AnchorPointY)));
                        xFrame.Add(new XAttribute("texture", destFilePath));
                    }
                }
            }
            xDoc.Save(pubModelFile);
            #endregion
        }

        /// <summary>
        /// 发布模型文件
        /// </summary>
        [Obsolete("已过期，请修改")]
        public static void Publish(string publishPath, AGModel model)
        {
            //string modelFolder = project.GetFolder(model);
            //string modelFile = string.Format("{0}\\{1}.xml", project.GetFolder(), model.UniqueId);
            string pubModelFolder = string.Format("{0}\\{1:d4}", publishPath, model.Id);
            string pubModelFile = string.Format("{0}\\{1:d4}.xml", publishPath, model.Id);

            if (!Directory.Exists(pubModelFolder))
            {
                Directory.CreateDirectory(pubModelFolder);
            }

            #region // 拷贝文件
            for (int indexAction = 0; indexAction < model.Actions.Count; indexAction++)
            {
                AGAction objAction = model.Actions[indexAction];
                for (int indexDirection = 0; indexDirection < objAction.Directions.Count; indexDirection++)
                {
                    AGDirection objDirection = objAction.Directions[indexDirection];
                    if (objDirection.RefDirection != null)
                    {
                        continue;
                    }
                    for (int indexFrame = 0; indexFrame < objDirection.Frames.Count; indexFrame++)
                    {
                        AGFrame objFrame = objDirection.Frames[indexFrame];
                        if (objFrame.RefFrame != null)
                        {
                            continue;
                        }
                        string sourceFilePath = objFrame.ImageFileName;// string.Format("{0}\\{1}", modelFolder, objFrame.ImageFileName);
                        FileInfo sourceFileInfo = new FileInfo(sourceFilePath);
                        string destFilePath = string.Format("{0}\\{1:d4}-{2:d2}-{3:d2}-{4:d2}{5}",
                            pubModelFolder,
                            model.Id,
                            objAction.Id,
                            objDirection.Id,
                            indexFrame,
                            sourceFileInfo.Extension);
                        File.Copy(sourceFilePath, destFilePath, true);
                    }
                }
            }
            #endregion

            #region 生成模型文件
            XDocument xDoc = new XDocument();
            XElement xModel = new XElement("model");
            xModel.Add(new XAttribute("id", model.Id));
            xModel.Add(new XAttribute("caption", model.Caption));
            xModel.Add(new XAttribute("version", "1.1"));
            xDoc.Add(xModel);

            for (int indexAction = 0; indexAction < model.Actions.Count; indexAction++)
            {
                AGAction objAction = model.Actions[indexAction];
                XElement xAction = new XElement("action");
                xModel.Add(xAction);

                xAction.Add(new XAttribute("id", objAction.Id));

                for (int indexDirection = 0; indexDirection < objAction.Directions.Count; indexDirection++)
                {
                    AGDirection objDirection = objAction.Directions[indexDirection];
                    // 真实的输出方向，当是引用模式的时候指定到引用的方向上
                    AGDirection actualDirection = objDirection;
                    if (objDirection.RefDirection != null)
                    {
                        actualDirection = objDirection.RefDirection;
                    }
                    List<AGFrame> frames = objDirection.GetFrames();

                    XElement xDirection = new XElement("direction");
                    xAction.Add(xDirection);

                    xDirection.Add(new XAttribute("id", objDirection.Id));

                    for (int indexFrame = 0; indexFrame < frames.Count; indexFrame++)
                    {
                        AGFrame objFrame = frames[indexFrame];
                        XElement xFrame = new XElement("frame");
                        xDirection.Add(xFrame);

                        string sourceFilePath = objFrame.ImageFileName;// string.Format("{0}\\{1}", modelFolder, objFrame.ImageFileName);
                        FileInfo sourceFileInfo = new FileInfo(sourceFilePath);
                        string destFilePath = string.Format("{0:d4}-{1:d2}-{2:d2}-{3:d2}",
                            model.Id,
                            objAction.Id,
                            actualDirection.Id,
                            indexFrame);
                        xFrame.Add(new XAttribute("id", indexFrame));
                        //xFrame.Add(new XAttribute("width", objFrame.Width));
                        //xFrame.Add(new XAttribute("height", objFrame.Width));

                        xFrame.Add(new XAttribute("rect", string.Format("0,0,{0},{1}", objFrame.Width, objFrame.Height)));
                        xFrame.Add(new XAttribute("pivot", string.Format("{0},{1}", objFrame.AnchorPointX, objFrame.AnchorPointY)));
                        //xFrame.Add(new XAttribute("anchor-x", objFrame.AnchorPointX));
                        //xFrame.Add(new XAttribute("anchor-y", objFrame.AnchorPointY));
                        xFrame.Add(new XAttribute("path", destFilePath));
                    }
                }
            }
            xDoc.Save(pubModelFile);
            #endregion
        }
    }
}